/**
* Class to manage a request to get values from a known device: to be executed in the Servlet.
* An example could be following call:
* http://localhost:3141/idbcrownINServer/idbcrownINServer?action=GET_VALUES&InfoDevice=IR&InfoMeasure=Heat
*
* Project: idbcrown
* Author:  TM
* Date:    14/02/2013
*
* History
* ------------------
* 14/02/2013 - TM - Initial version.
*/
 
 
package Functions;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import Application.*;
import comm.*;
import Util.*;

public class getValuesFromDevice extends genericFunction implements constantsInterface
{
   /** Constants */
   protected static String LOGO_PATH           = libApplication.getInstance().getConfigValue("LOGO_PATH");
   protected static String VALUES_PATTERN_HTML = libApplication.getInstance().getConfigValue("VALUES_PATTERN_HTML");
   protected static String LOGO_IN_HTML        = "#valuelogoPath#";
   protected static String DEVICE_IN_HTML      = "#valueIdDevice#";
   protected static String MEASURE_IN_HTML     = "#valueIdMeasure#";
   protected static String RESULT_IN_HTML      = "#valueResults#";   
   /** Variables */
   protected String valueIdDevice;
   protected String valueIdMeasure;

     /** Constructor  */
    public getValuesFromDevice(HttpServletRequest _request, HttpServletResponse _response) throws IOException, ServletException
    {
       super(_request, _response, true); // This functions should work as a XML answer and not HTML
    }
    
    /** Execute function */
    public String execute()
    {
       String  result  = "";
       boolean isOk    = true;
       valueIdDevice   = getRequest().getParameter(idDevice);     
       valueIdMeasure  = getRequest().getParameter(idMeasure);            
       
       debug("Asking values for idDevice "+valueIdDevice+"/idMeasure "+valueIdMeasure);
      
       try
       {
         debug("Asked value excuted with result: "+isOk);        
         String[] values = new String[]{"30", "45"};
         result = values[0] + ";" + values[1];         
       }
       catch(Exception ex)
       {
         sendError(ex);
       }
       
       return result;
    }
    
    /** Calls to the DBServer to get values */
    protected void callDBServerGetValuesFromDevice()
    {
       servletDBComm dbServlet   = servletDBComm.getInstance();
       String        actionParam = getValuesAction;
       String[]      names       = {idDevice,      idMeasure};
       String[]      values      = {valueIdDevice, valueIdMeasure};
       try
       {
         String params = dbServlet.getFormattedParameters(actionParam, names, values);
         String result = dbServlet.post_get(params);
         sendResponse(result);
       }
       catch(Exception ex)
       {
         sendError(ex);
       }
    }
    
    /** Becomes XML the answer received by server */
    protected String getXMLAnswer(String[] values)
    {
       String result = "";
       
       result  = htmlTools.openTag (DEVICE_TAG)  + valueIdDevice;
       result += htmlTools.openTag (MEASURE_TAG) + valueIdMeasure;
       
       for (int i=0; i<values.length;i++)       
         result += htmlTools.openCloseTag(VALUE_TAG, values[i]);
       
       result += htmlTools.closeTag(MEASURE_TAG);
       result += htmlTools.closeTag(DEVICE_TAG);
       
       return result;
    }
    
    /** REDEFINED: Write content in output web page */
    protected void writeHTMLResponse(String result) throws IOException, ServletException
    {
      String resultHTML  = "";
      File   patternHTML = new File(VALUES_PATTERN_HTML);
      
      if (!patternHTML.exists())
        result = getDefaultResponse(result);
      else
      {
        String[] lines = libFileUtil.readFileAsArrayString(patternHTML);
        
        for (int i=0; i<lines.length; i++)
        {
          String currLine = lines[i];
          if ((currLine != null) && (currLine.trim().length() > 0))
          {
            currLine = currLine.replaceAll(LOGO_IN_HTML,    LOGO_PATH);
            currLine = currLine.replaceAll(DEVICE_IN_HTML,  valueIdDevice);
            currLine = currLine.replaceAll(MEASURE_IN_HTML, valueIdMeasure);
            currLine = currLine.replaceAll(RESULT_IN_HTML,  result);
            resultHTML = resultHTML + currLine + "\n";           
          }
        }
      }      
      
      out.println(resultHTML);
    }
    
    /** Gets the default result if there is not pattern file */
    protected String getDefaultResponse(String result) throws IOException, ServletException
    {
       return "<html><head><title>Thank You to use idbcrownINServer (by ITCrown, 2013)</title></head>"+
              "<body bgcolor=\"white\"><body><IMG SRC=\""+LOGO_PATH+"\">" +      
              "<h1> Requesting values for device "+valueIdDevice+"</h1>" +
              "<BR><h2> Measure detected for "+valueIdMeasure+"</h2><BR>"+result+"</body></html>";
    }   
    
}